#    SUSAN® - Sole of Unix Save ANything
#
#   Copyright (C) 2017-2020 Hechzh Property
#
#   This program is Free Software; you can redistribute it and/or
#   modify it under the terms of version three of the GNU Affero General Public
#   License as published by the Free Software Foundation and included
#   in the file LICENSE.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301, USA.
message("Entering ${CMAKE_CURRENT_SOURCE_DIR}")

if(HAVE_WIN32)
  include_directories(
    ${PROJECT_SOURCE_DIR}/src/win32/generic
    ${PROJECT_SOURCE_DIR}/src/win32/stored
    ${PROJECT_SOURCE_DIR}/src/stored/backends
  )
endif()

# BACKENDS  ####
if(NOT build_client_only)
  set(BACKENDS "")
  list(APPEND BACKENDS unix_tape_device.d)
  list(APPEND BACKENDS unix_fifo_device.d)
  if(${HAVE_CEPHFS})
    list(APPEND BACKENDS rados_device.d)
  endif()
  if(${HAVE_GLUSTERFS})
    list(APPEND BACKENDS gfapi_device.d)
  endif()
  if(TARGET droplet)
    list(APPEND BACKENDS droplet_device.d)
  endif()
  set(BACKENDS
      ${BACKENDS}
      PARENT_SCOPE
  )
  message("Backends are now ${BACKENDS}")
endif()

if(NOT ${HAVE_DYNAMIC_SD_BACKENDS})
  set(AVAILABLE_DEVICE_API_SRCS backends/generic_tape_device.cc)

  if(HAVE_WIN32)
    list(
      APPEND
      AVAILABLE_DEVICE_API_SRCS
      ${PROJECT_SOURCE_DIR}/src/win32/stored/backends/win32_fifo_device.cc
      ${PROJECT_SOURCE_DIR}/src/win32/stored/backends/win32_file_device.cc
      ${PROJECT_SOURCE_DIR}/src/win32/stored/backends/win32_tape_device.cc
    )
  else()
    list(APPEND AVAILABLE_DEVICE_API_SRCS backends/unix_fifo_device.cc
         # backends/droplet_device.cc
         backends/unix_tape_device.cc backends/unix_file_device.cc
    )
  endif()

  if(${HAVE_CEPHFS})
    list(APPEND AVAILABLE_DEVICE_API_SRCS backends/cephfs_device.cc)
  endif()

  if(${HAVE_GFAPI})
    list(APPEND AVAILABLE_DEVICE_API_SRCS backends/gfapi_device.cc)
  endif()

  if(${HAVE_RADOS})
    list(APPEND AVAILABLE_DEVICE_API_SRCS backends/rados_device.cc)
  endif()
  if(TARGET droplet)
    list(APPEND AVAILABLE_DEVICE_API_SRCS backends/droplet_device.cc)
    list(APPEND AVAILABLE_DEVICE_API_SRCS backends/chunked_device.cc)
  endif()
endif()

set(LIBSUSANSD_SRCS
    acquire.cc
    ansi_label.cc
    askdir.cc
    autochanger.cc
    autochanger_resource.cc
    block.cc
    bsr.cc
    butil.cc
    crc32/crc32.cc
    dev.cc
    device.cc
    device_control_record.cc
    device_resource.cc
    ebcdic.cc
    label.cc
    lock.cc
    mount.cc
    read_record.cc
    record.cc
    reserve.cc
    scan.cc
    sd_backends.cc
    sd_device_control_record.cc
    sd_plugins.cc
    sd_stats.cc
    spool.cc
    stored_globals.cc
    stored_conf.cc
    vol_mgr.cc
    wait.cc
    ${AVAILABLE_DEVICE_API_SRCS}
)

set(SDSRCS
    append.cc
    askdir.cc
    authenticate.cc
    dir_cmd.cc
    fd_cmds.cc
    job.cc
    mac.cc
    ndmp_tape.cc
    read.cc
    sd_cmds.cc
    sd_stats.cc
    socket_server.cc
    status.cc
)

if(HAVE_WIN32)
  list(APPEND SDSRCS stored.cc ../win32/generic/main.cc
       ../win32/generic/service.cc ../win32/stored/storedres.rc
  )
endif()

set(BTAPESRCS btape.cc)
if(HAVE_WIN32)
  list(APPEND BTAPESRCS ../win32/stored/btaperes.rc)
endif()

set(BLSSRCS bls.cc)
if(HAVE_WIN32)
  list(APPEND BLSSRCS ../win32/stored/blsres.rc)
endif()

set(BEXTRACTSRS bextract.cc)
if(HAVE_WIN32)
  list(APPEND BEXTRACTSRS ../win32/stored/bextractres.rc)
endif()

set(BSCANSRCS bscan.cc)
if(HAVE_WIN32)
  list(APPEND BSCANSRCS ../win32/stored/bscanres.rc)
endif()

set(BCOPYSRCS bcopy.cc)

set(STORED_RESTYPES autochanger device director ndmp messages storage)

set(BACKEND_OBJECTS "") # backend shared objects

if(${HAVE_DYNAMIC_SD_BACKENDS})
  if(${HAVE_RADOS})
    list(APPEND BACKEND_OBJECTS libsusansd-rados)
  endif()
  if(${HAVE_CEPHFS})
    list(APPEND BACKEND_OBJECTS libsusansd-cephfs)
  endif()
  if(GFAPI_LIBRARIES)
    list(APPEND BACKEND_OBJECTS libsusansd-gfapi)
  endif()

  if(TARGET droplet)
    list(APPEND BACKEND_OBJECTS libsusansd-droplet)
    list(APPEND BACKEND_OBJECTS libsusansd-chunked)
  endif()

  set(LIBSUSANSD_LIBRARIES susan ${BACKEND_OBJECTS})
  message(STATUS "BACKEND_OBJECTS ARE ${BACKEND_OBJECTS}")
else()
  if(TARGET droplet)
    list(APPEND LIBSUSANSD_LIBRARIES droplet)
  endif()

  if(${HAVE_RADOS})
    list(APPEND LIBSUSANSD_LIBRARIES ${RADOS_LIBRARIES})
  endif()
  if(${HAVE_CEPHFS})
    list(APPEND LIBSUSANSD_LIBRARIES ${CEPHFS_LIBRARIES})
  endif()
  if(${HAVE_GFAPI})
    list(APPEND LIBSUSANSD_LIBRARIES ${GFAPI_LIBRARIES})
  endif()
  set(LIBSUSANSD_LIBRARIES susan ${LIBSUSANSD_LIBRARIES})
  message(STATUS "LIBSUSANSD_LIBRARIES ARE ${LIBSUSANSD_LIBRARIES}")
endif()

if(${HAVE_DYNAMIC_SD_BACKENDS})
  list(APPEND LIBSUSANSD_SRCS backends/unix_file_device.cc)
endif()

add_library(susansd SHARED ${LIBSUSANSD_SRCS})

if(NOT ${HAVE_DYNAMIC_SD_BACKENDS})
  target_link_libraries(susansd ${LIBSUSANSD_LIBRARIES})
endif()

if(HAVE_DARWIN_OS)
  target_link_libraries(susansd susan)
endif()

add_library(stored_objects STATIC ${SDSRCS})
target_link_libraries(stored_objects ${NDMP_LIBS})

add_executable(susan-sd stored.cc)

set(SUSAN_SD_LIBRARIES susan susansd susanfind ${NDMP_LIBS})

if(HAVE_WIN32)
  target_sources(susan-sd PRIVATE ../win32/generic/main.cc)
  list(APPEND SUSAN_SD_LIBRARIES comctl32)
endif()

target_link_libraries(susan-sd stored_objects ${SUSAN_SD_LIBRARIES})

add_executable(bls ${BLSSRCS})
target_link_libraries(bls susan susansd susanfind)

add_executable(bextract ${BEXTRACTSRS})
target_link_libraries(bextract susansd susanfind susan)

add_executable(bscan ${BSCANSRCS})
target_link_libraries(bscan susan susansd susanfind susansql susancats)

add_executable(btape ${BTAPESRCS})
target_link_libraries(btape susansd susan)

add_executable(bcopy ${BCOPYSRCS})

target_link_libraries(bcopy susansd susan)

install(TARGETS susansd DESTINATION ${libdir})

install(TARGETS susan-sd bls bextract bscan btape bcopy
        DESTINATION "${sbindir}"
)

install(CODE "set(BACKENDS \"${BACKENDS}\")")
install(CODE "set(configtemplatedir \"${configtemplatedir}\")")
install(CODE "set(SRC_DIR \"${PROJECT_SOURCE_DIR}\")")
install(SCRIPT ${PROJECT_SOURCE_DIR}/cmake/install-stored-configfiles.cmake)

install(DIRECTORY DESTINATION "${archivedir}")

if(${HAVE_DYNAMIC_SD_BACKENDS})
  add_subdirectory(backends)
endif()

if(TARGET droplet)
  target_compile_definitions(susansd PRIVATE HAVE_SUSANSD_DROPLET_DEVICE)
endif()
